Page({
  data: {
    id: '',
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    fileList: [],
    houseName: '',
    repairItemName: "",

    // 新增需要用到数据
    "houseId": "",
    "repairItemId": "",
    "mobile": "",
    "appointment": "",
    "description": ""
  },
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  onLoad({
    id
  }) {
    if (id) {
      // 修改
      this.setData({
        id
      })
      wx.setNavigationBarTitle({
        title: '修改报修信息',
      })
      // 数据回显
      this.getData()
    } else {
      // 新增
      wx.setNavigationBarTitle({
        title: '新增报修信息',
      })
    }
    this.getHouseList()
    this.getRepairItem()
  },
  // 根据id获取详情数据回显
  async getData() {
    const res = await wx.http.get('/repair/' + this.data.id)
    this.setData({
      appointment: res.data.appointment,
      fileList: res.data.attachment,
      description: res.data.description,
      houseId: res.data.houseId,
      houseName: res.data.houseInfo,
      mobile: res.data.mobile,
      repairItemId: res.data.repairItemId,
      repairItemName: res.data.repairItemName,
      status: res.data.status,
    })
    console.log(res);
  },


  // 获取房屋列表
  async getHouseList() {
    const res = await wx.http.get('/room')
    this.setData({
      houseList: res.data.map(item => {
        return {
          name: item.point + item.building + '号楼' + item.room,
          id: item.id
        }
      })
    })
    console.log(res);
  },
  // 选择房屋
  selectHouse(e) {
    console.log(e);
    this.setData({
      houseName: e.detail.name,
      houseId: e.detail.id
    })
  },
  // 获取维修项目
  async getRepairItem() {
    const res = await wx.http.get('/repairItem')
    console.log('维修项目', res);
    this.setData({
      repairItem: res.data
    })
  },
  // 选择维修项目
  selectReapir(e) {
    console.log(e);
    this.setData({
      repairItemName: e.detail.name,
      repairItemId: e.detail.id
    })
  },
  // 时间选择确定点击
  dateConfirm(e) {
    console.log(wx.utils.formatTime(e.detail));
    // 关闭弹窗
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatTime(e.detail)
    })
  },
  dateCancel() {
    // 关闭弹窗
    this.setData({
      dateLayerVisible: false
    })
  },
  afterRead(e) {
    console.log('图片临时路径', e);
    const fileUrl = e.detail.file.url
    // wx.uploadFile
    wx.uploadFile({
      filePath: fileUrl,
      name: 'file',
      url: wx.baseURL + '/upload',
      header: {
        Authorization: getApp().token
      },
      success: (res) => {
        console.log('上传成功', res);
        const obj = JSON.parse(res.data)
        this.data.fileList.push(obj.data)
        this.setData({
          fileList: this.data.fileList
        })
      }
    })
  },
  // 删除上传的图片
  delImg(e) {
    console.log(e);
    this.data.fileList.splice(e.detail.index, 1)
    this.setData({
      fileList: this.data.fileList
    })
  },
  // 提交报修确定点击
  async submit() {
    // 验证报修房屋是否选择
    if (!this.data.houseId) {
      return wx.utils.toast('请先选择房屋')
    }
    // 验证是否选择维修项目
    if (!this.data.repairItemId) {
      return wx.utils.toast('请先选择维修项目')
    }
    // 验证手机号
    const reg = /^1[3-9]\d{9}$/
    if (!reg.test(this.data.mobile.trim())) {
      return wx.utils.toast('请正确输入手机号')
    }
    // 验证预约日期
    if (!this.data.appointment) {
      return wx.utils.toast('请先选择预约日期')
    }
    // 验证问题描述
    if (!this.data.description) {
      return wx.utils.toast('请先输入问题描述')
    }
    // 问题附件
    if (!this.data.fileList.length) {
      return wx.utils.toast('请先上传问题附件')
    }
    // 调用新增报修接口
    const params = {
      "houseId": this.data.houseId,
      "repairItemId": this.data.repairItemId,
      "mobile": this.data.mobile,
      "appointment": this.data.appointment,
      "description": this.data.description,
      "attachment": this.data.fileList
    }
    if (this.data.id) {
      params.id = this.data.id
    }
    await wx.http.post('/repair', params)
    // 提示
    wx.showToast({
      title: this.data.id ? '修改报修成功' : '报修成功',
    })
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })

  }


})